<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/9/25
 * Time: 10:41
 */

namespace app\api\controller;


use think\cache\driver\Redis;
use think\Controller;
use think\Db;
header("ACCESS-CONTROL-ALLOW-ORIGIN:http://114.215.68.205");
class History extends Controller
{
    public function _initialize(){
        //验证加密
        $VALUE_KEY = ['afewfewfew21342dsaewf'=>'8f4da4f9ew4f98ewq65fwe48f9w'];
        $HEADER_TIME = isset($_SERVER['HTTP_TIME']) ? $_SERVER['HTTP_TIME'] : '';
        $HEADER_KEY = isset($_SERVER['HTTP_KEY']) ? $_SERVER['HTTP_KEY'] : '';
        $HEADER_SIGN = isset($_SERVER['HTTP_SIGN']) ? $_SERVER['HTTP_SIGN'] : '';

        $POST_LOGIN_KEY = isset($_SERVER['HTTP_LOGINKEY']) ? $_SERVER['HTTP_LOGINKEY'] : ( isset($_POST['login_key']) ? $_POST['login_key'] : '' );
        $POST_NAME = isset($_SERVER['HTTP_USERNAME']) ? $_SERVER['HTTP_USERNAME'] : (isset($_POST['username']) ? $_POST['username'] : (isset($_POST['name']) ? $_POST['name']:''));


        if( !isset($VALUE_KEY[$HEADER_KEY]) ){
            exit(json_encode(['result'=>0], JSON_UNESCAPED_UNICODE));
        }

        $validation =  $HEADER_TIME.$VALUE_KEY[$HEADER_KEY].$POST_LOGIN_KEY.$POST_NAME;
        if( $HEADER_SIGN != md5($validation) ){
            exit(json_encode(['result'=>0], JSON_UNESCAPED_UNICODE));
        }
    }

    public function index(){
        $czid = input( 'czid', 0 );
        $ret = array(
            'result'=>0,
            'ResultMsg' => ''
        );
        if( empty($czid) ){
            $ret['ResultMsg'] = '请求参数错误';
            return json_encode($ret,JSON_UNESCAPED_UNICODE);
        }
        $redis = new Redis();
        $next_redis = $redis->get($czid);

        $datas = ['next_no'=>'','next_time'=>0,'history'=>[]];
        if( $next_redis ){
            $next_arr = explode(',', $next_redis);
            $datas['next_no'] = $next_arr[0];
            $datas['next_time'] = $next_arr[1];
        }else{
            $next_detail = Db::name('kjjg_list')->where(['czid'=>$czid,'kjjg'=>0])->field("qihao,kjjg,jztime")->order('jztime','DESC')->find();
            if( $next_detail ){
                $datas['next_no'] = $next_detail['qihao'];
                $datas['next_time'] = $next_detail['jztime'];
            }
        }
        $list = Db::name('kjjg_list')->where('czid',$czid)->where('kjjg','<>','0')->field("qihao,kjjg,jztime")->order('jztime','DESC')->paginate(20)->each(function($v)use($czid){
            $v['kjjg'] = kjjg_split($v['kjjg'],$czid);
            return $v;
        })->toArray();
        $url = Db::name('caipai_list')->where('id',$czid)->value('logo');
        $list && $datas['history'] = $list['data'];
        $ret['result'] = 1;
        $ret['datas'] = $datas;
        $ret['datas']['url'] = $_SERVER['REQUEST_SCHEME'].'://'.$_SERVER['SERVER_ADDR'].'/'.$url;
        //print_r($_SERVER);
        return json_encode($ret,JSON_UNESCAPED_UNICODE);
    }

}